﻿using Corina.Datamodel.Entities;
using Corina.Enumerations;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Corina.DataContext.Service
{
    public class AutoResolveService : ServiceBase
    {
        public DataTable GetAllItems(string connectionString, int deviceKindId, string deviceSn, int index, int size, ref int count)
        {
            List<SqlParameter> paras = new List<SqlParameter>();
            string qstring = "SELECT * FROM ( ";
            string cstring = "SELECT COUNT(*) FROM ( ";
            StringBuilder builder = new StringBuilder();


            builder.Append("SELECT ROW_NUMBER() OVER (ORDER BY tar.DeviceSn) AS ROW_NUMBER, tar.*, td.DeviceKindId, tdk.Name AS Kind ");
            builder.Append("  FROM t_auto_resolve AS tar ");
            builder.Append("LEFT JOIN t_device AS td ON td.DeviceSn = tar.DeviceSn ");
            builder.Append("LEFT JOIN t_device_kind AS tdk ON tdk.Id = td.DeviceKindId ");
            builder.Append("WHERE 1=1 ");

            #region Condition
            builder.Append("AND td.DeviceSn LIKE @DeviceSn ");
            paras.Add(new SqlParameter("@DeviceSn", string.Format("%{0}%", deviceSn)));

            if (deviceKindId > 0)
            {
                builder.Append("AND tdk.Id = @KindId ");
                paras.Add(new SqlParameter("@KindId", deviceKindId));
            } 
            #endregion

            builder.Append(") AS query ");

            count = DbOperator.GetCount(connectionString, string.Format("{0}{1}", cstring, builder), paras.ToArray());

            builder.Append("WHERE query.[ROW_NUMBER] BETWEEN @Index + 1 AND @Index + @Size ");
            paras.Add(new SqlParameter("@Index", (index - 1) * size));
            paras.Add(new SqlParameter("@Size", size));
            builder.Append("ORDER BY query.[ROW_NUMBER]");

            DataTable table = DbOperator.GetDataTable(connectionString, string.Format("{0}{1}", qstring, builder), paras.ToArray());
            return table;
        }

        public ResultValue Insert(string connectionString, AutoResolveInfo entity)
        {
            if (ChechExist(connectionString, entity.DeviceSn))
            {
                return ResultValue.Exists;
            }
            bool flag = DbOperator.UpdateEntity(connectionString, entity, EntityOperation.Insert);

            return flag ? ResultValue.Succeed : ResultValue.Failed;
        }

        public bool Update(string connectionString, int id, bool flag)
        {
            return DbOperator.UpdateEntityById<AutoResolveInfo>(connectionString, id, new List<string>() { "AutoResolve" }, new List<object>() { flag });
        }

        public bool Remove(string connectionString, int id)
        {
            return DbOperator.RemoveEntityById<AutoResolveInfo>(connectionString, id);
        }

        public bool CheckFlag(string connectionString, string deviceSn)
        {
            return DbOperator.Exists<AutoResolveInfo>(connectionString, new List<string>() { "DeviceSn", "AutoResolve" }, new List<object>() { deviceSn, true });
        }

        private bool ChechExist(string connectionString, string deviceSn)
        {
            return DbOperator.Exists<AutoResolveInfo>(connectionString, new List<string>() { "DeviceSn" }, new List<object>() { deviceSn });
        }
    }
}
